<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线五子棋登录</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <style>
        /* 基础样式 */
        * {
            box-sizing: border-box;
        }
        
        body {
            margin: 0;
            padding: 0;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        .nav {
            height: 50px;
            background-color: rgb(50, 50, 50);
            color: white;
            line-height: 50px;
            padding-left: 20px;
            font-size: 18px;
        }
        
        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: calc(100vh - 50px);
            padding: 20px;
        }
        
        .login-dialog {
            width: 100%;
            max-width: 450px;
            background-color: rgba(255, 255, 255, 0.92);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            padding: 25px;
            animation: fadeInUp 0.6s ease-out;
        }
        
        .login-dialog h3 {
            text-align: center;
            padding: 10px 0 25px;
            font-size: 28px;
            color: #333;
            margin: 0;
        }
        
        /* 输入框容器 */
        .input-container {
            position: relative;
            margin: 20px 0;
            width: 100%;
        }
        
        .input-container input {
            width: 100%;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            background-color: rgba(255, 255, 255, 0.9);
            transition: all 0.3s ease;
        }
        
        .input-container input:focus {
            border-color: #4CAF50;
            box-shadow: 0 0 8px rgba(76, 175, 80, 0.3);
            outline: none;
        }
        
        .input-container label {
            position: absolute;
            top: 15px;
            left: 15px;
            color: #777;
            pointer-events: none;
            transition: all 0.3s ease;
            background-color: transparent;
            padding: 0 5px;
        }
        
        .input-container input:focus+label,
        .input-container input:not(:placeholder-shown)+label {
            top: -10px;
            left: 10px;
            font-size: 14px;
            color: #4CAF50;
            background-color: rgba(255, 255, 255, 0.9);
        }
        
        /* 按钮样式 */
        #submit {
            transition: all 0.3s ease;
            transform: translateY(0);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            background-color: #81C784;
            width: 100%;
            height: 50px;
            color: white;
            border: none;
            outline: none;
            border-radius: 8px;
            margin-top: 10px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
        }
        
        #submit:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
            background-color: #66BB6A;
        }
        
        #submit:active {
            transform: translateY(1px);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            background-color: #4CAF50;
        }
        
        /* 链接样式 */
        .link-container {
            margin-top: 15px;
            text-align: center;
        }
        
        .link-container a {
            color: #4CAF50;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s ease;
        }
        
        .link-container a:hover {
            color: #388E3C;
            text-decoration: underline;
        }
        
        /* 错误信息样式 */
        .error-message {
            display: none;
            background-color: #ffebee;
            color: #f44336;
            padding: 12px;
            border-radius: 8px;
            margin: 10px 0;
            text-align: center;
            animation: fadeIn 0.3s ease-out;
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 480px) {
            .login-container {
                padding: 15px;
            }
            
            .login-dialog {
                padding: 20px;
            }
            
            .login-dialog h3 {
                font-size: 24px;
                padding: 5px 0 20px;
            }
            
            .input-container {
                margin: 15px 0;
            }
            
            .input-container input {
                padding: 12px;
                font-size: 15px;
            }
            
            .input-container label {
                font-size: 14px;
            }
            
            #submit {
                height: 45px;
                font-size: 16px;
            }
            
            .link-container {
                font-size: 14px;
            }
        }
        
        @media (max-width: 360px) {
            .login-dialog {
                padding: 15px;
            }
            
            .login-dialog h3 {
                font-size: 22px;
            }
            
            .input-container input {
                padding: 10px;
                font-size: 14px;
            }
            
            .input-container label {
                font-size: 13px;
            }
            
            #submit {
                height: 42px;
                font-size: 15px;
            }
        }
    </style>
</head>

<body>
    <div class="nav">
        在线五子棋登录页面
    </div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>登录</h3>

            <!-- 错误信息展示区域 -->
            <div id="error-message" class="error-message"></div>

            <div class="input-container">
                <input type="text" id="user_name" placeholder=" ">
                <label for="user_name">用户名</label>
            </div>

            <div class="input-container">
                <input type="password" id="password" placeholder=" ">
                <label for="password">密码</label>
            </div>

            <button id="submit">登录</button>

            <div class="link-container">
                <a href="/register.html">没有账号？点击注册</a>
            </div>
        </div>
    </div>

    <script src="./js/jquery.min.js"></script>
    <script>
        // 绑定登录按钮事件
        document.getElementById('submit').addEventListener('click', login);
        
        function login() {
            // 清空错误信息
            document.getElementById('error-message').style.display = 'none';

            var login_info = {
                username: document.getElementById("user_name").value,
                password: document.getElementById("password").value
            };

            $.ajax({
                url: "/login",
                type: "post",
                data: JSON.stringify(login_info),
                success: function (result) {
                    // 登录成功直接跳转
                    window.location.assign("/game_hall.html");
                },
                error: function (xhr) {
                    // 从响应中提取错误信息
                    let errorMsg = "登录失败，请重试";
                    if (xhr.responseJSON && xhr.responseJSON.reason) {
                        errorMsg = xhr.responseJSON.reason;
                    } else if (xhr.status === 400) {
                        errorMsg = "用户名或密码错误";
                    }

                    // 在页面上显示错误信息
                    const errorElement = document.getElementById('error-message');
                    errorElement.textContent = errorMsg;
                    errorElement.style.display = 'block';

                    // 清空密码框
                    document.getElementById("password").value = "";

                    // 5秒后自动隐藏错误信息
                    setTimeout(() => {
                        errorElement.style.display = 'none';
                    }, 5000);
                }
            })
        }
    </script>
</body>

</html>